package com.ngkj.zmfptback.business.service.write.impl;

import cn.hutool.core.lang.UUID;
import com.jcraft.jsch.ChannelSftp;
import com.jcraft.jsch.JSchException;
import com.jcraft.jsch.SftpATTRS;
import com.jcraft.jsch.SftpException;
import com.ngkj.zmfptCommon.common.sftp.SFTPChannel;
import com.ngkj.zmfptCommon.common.sftp.SFTPConstants;
import com.ngkj.zmfptback.business.bean.Image;
import com.ngkj.zmfptback.business.bean.RentalRequestEntiry;
import com.ngkj.zmfptback.business.mapper.write.RentalWriteMapper;
import com.ngkj.zmfptback.business.service.write.ImageWriteService;
import com.ngkj.zmfptback.business.service.write.RentalWriteService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * @author ChenHN
 * @since 2021/12/31 creation
 **/
@Service
@Transactional(propagation = Propagation.REQUIRED)
public class RentalWriteServiceImpl implements RentalWriteService {

    @Resource
    private RentalWriteMapper rentalWriteMapper;

    @Resource
    private ImageWriteService imageWriteService;

    @Resource
    private SFTPChannel sftpChannel;

    @Override
    public void agreeRentals(List<String> list) {
        rentalWriteMapper.agreeRentals(list);
    }

    @Override
    public void agreeAllRental() {
        rentalWriteMapper.agreeAllRental();
    }

    @Override
    public void refusedRentals(List<String> list) {
        rentalWriteMapper.refusedRentals(list);
    }

    @Override
    public void addRentalInfo(RentalRequestEntiry rentalRequestEntiry,MultipartFile[] files) throws Exception {
        rentalRequestEntiry.setFbsj(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date()));
        rentalWriteMapper.addRentalInfo(rentalRequestEntiry);
        //存储图片到服务器
        Map<String, String> connectParameter = SFTPConstants.getConnectParameter();
        ChannelSftp channel = sftpChannel.getChannel(connectParameter, SFTPConstants.TIMEOUT);
        StringBuilder dir = new StringBuilder("/usr/local/static/images/business");
        String time = new SimpleDateFormat("yyyyMMdd").format(new Date());
        dir.append("/" + time);

        SftpATTRS attrs = null;
        try {
            attrs = channel.stat(dir.toString());
        } catch (Exception e) {
            // TODO: handle exception
        }
        if (attrs == null) {
            channel.mkdir(dir.toString());
        }
        for (MultipartFile multipartFile : files) {
            String originalFilename = multipartFile.getOriginalFilename();
            String file = dir + "/" + UUID.randomUUID().toString().replace("-", "") + "_" + originalFilename;
            channel.put(multipartFile.getInputStream(), file, ChannelSftp.OVERWRITE);
            Image image = new Image();
            image.setType("02");
            image.setName(originalFilename);
            String[] split = file.split("/static");
            image.setPath(split[1]);
            image.setTypeId(rentalRequestEntiry.getCzfxxlsh());
            imageWriteService.addImage(image);
        }
        sftpChannel.closeChannel();
    }

    @Override
    public void cancelRental(Long czfxxlsh) {
        rentalWriteMapper.cancelRental(czfxxlsh);
    }
}
